import util  from '../../utils/util.js'
import api from '../../api/api.js'
const {
  $Toast
} = require('../../dist/base/index');
Page({
  data: {
    Page:true,
    pg:true,
    deviceWidth: 0,
    deviceHeight: 0,
    selectTabindex:0,
    classPage:false,
    activeCss:"background-color:#8F272E;color:#fff;",
    normalCss:"background-color:#fff;color:#000;",
   
    categoryType: 1, //1是推荐界面  0是其他一级分类界面
    firstCategoryList: [

    ],//左侧菜单
    recommendPageImage: [{
        "url": "../../images/chanel.jpg"
    }],  //右边推荐大图的数组，及参数
    recommendList: [

    ], //推荐商品
    
    normalList: {
       
    },//正常商品清单
    curentItem: {


    }, 
  },
  onLoad: function (options) {
   
    var that = this;
    
    // 页面初始化 options为页面跳转所带来的参数
    util.getSystemInfo({
      success: (res) => {
        that.setData({
          deviceWidth: res.windowWidth,
          deviceHeight: res.windowHeight
        });
      }
    });
 
    
  },
  onReady: function () {
    // 页面渲染完成
    setInterval(this.interVal, 1000);
    let that=this;
    that.setData({
       firstCategoryList: util.JSONdata.tabtwo.left,

    }) 
    this.dialog = this.selectComponent("#dialog");
  
   
  },

  onShow: function () {
    // 页面显示
      
  let that =this;
  that.setData({
     recommendList:util.JSONdata.tabtwo.left[0].children
  })
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  updateCategoryListData:function (categoryListArray) {
    var that = this;
    var leftArray = this.data.firstCategoryList;
    var rightArray = this.data.secondCateGoryList;
    var objectRecommend = { name: '推荐' };
    leftArray.push(objectRecommend);
    for (let i = 0; i < categoryListArray.length; i++) {
      var object = categoryListArray[i];
      if (object.menu == '1') {
        leftArray.push(object);
      } else if (object.menu == '2') {
        rightArray.push(object);
      }
    }
   
    that.setData({
      firstCategoryList: leftArray,
      secondCateGoryList: rightArray,
    })
  },
  tapCategory: function (event) {
    var that = this;

    this.setData({
      selectTabindex:event.target.id
    })
 
    if (event.target.id!=0){
      that.setData({
        normalList:event.target.dataset.childrenitems,
        categoryType: 0,
      })
      
      
    }else{
      that.setData({
        leftOtherArray:event.target.dataset.childrenitems,
        categoryType:1,
      })
    }

    // if (event.target.dataset.id) {
    //   var firstCategoryList = that.data.firstCategoryList;
    //   var secondCategoryArray = that.data.secondCateGoryList;
    //   var mutableTemArray = that.data.leftOtherArray;
    //   mutableTemArray.splice(0, mutableTemArray.length);
    //   for (let i = 0; i < secondCategoryArray.length; i++) {
    //     var item = secondCategoryArray[i];
    //     if (item.parentId == event.target.dataset.id) {
    //       mutableTemArray.push(item);
    //     }
    //   }
    //   for (let i = 0; i < firstCategoryList.length; i++) {
    //     var item = firstCategoryList[i];
    //     if (event.target.dataset.id == item.id) {
    //       // that.setData({
    //       //   rightBigImageArray: item.adImg,
    //       // })
    //     }
    //   }
    //   that.setData({
    //     leftOtherArray: mutableTemArray,
    //   })

    // }
  },
  setSession(item) {
    var time = util.formatTime(new Date()).split(" ")[0];
    var that = this;
    item.createTime = time;
    try {
      var value = wx.getStorageSync('items');

      if (value) {
        var arr = new Array();
        for (let i in value) {

          arr.push(value[i].id);

        }
        if (arr.includes(item.id)) {
          $Toast({
            content: '收藏过了',
            type: 'warning'
          });
          return;
        } else {
          value.push(item);
          wx.setStorageSync("items", value);
          console.log(22);
          $Toast({
            content: '收藏成功',
            type: 'success'
          });
        }


      } else {



        var arr = new Array();;
        arr.push(item);
        wx.setStorageSync("items", arr);
        $Toast({
          content: '收藏成功',
          type: 'success'
        });

      }

    } catch (e) {

    }




  },
  //获取详情
  getCell(event) {
 
    this.setData({
      curentItem: event.currentTarget.dataset.detail
    })
    this.dialog.showDialog();
  },
  //取消事件 
  _cancelEvent() {

    this.dialog.hideDialog();
  },

  //确认事件 
  _confirmEvent() {


    this.setSession(this.data.curentItem)

     
    this.dialog.hideDialog();
  }, 
  interVal() {
    var got = Date.parse(new Date()) / 1000;

    if (got > util.heal) {
      
      this.setData({
        Page:false,
        pg: true
      })
    }else{
       this.setData({
         Page: true,
         pg: false
        })
    }
  }
})